package org.oncase.smcase.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.oncase.smcase.controller.ActivityController;
import org.oncase.smcase.entity.Activity;
import org.oncase.smcase.entity.Expense;
import org.oncase.smcase.entity.ExpenseType;
import org.oncase.smcase.entity.Frequency;
import org.oncase.smcase.entity.FrequencyUnit;
import org.oncase.smcase.entity.Person;
import org.oncase.smcase.exception.BSCException;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

public class ActivityAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String name;
	
	private String description;
	
	private String indicators;
	
	private String target;
	
	private long frequencyLength;
	
	private String frequencyUnit;
	
	private double[] expenseValue;
	
	private String[] expenseType;
	
	private String[] personsNames;
	
	private String action;
	
	private int activId;
	
	private int objId;
	
	private Map<String, Object> ajaxResponse = new HashMap<String, Object>();
	
	private ActivityController activityCon;
	
	@Inject
	public ActivityAction(ActivityController activityCon) {
		this.activityCon = activityCon;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println("aqui");
		if (this.getAction() != null && !this.getAction().trim().equals("")
				&& (this.getAction().trim().equals("insert") 
						|| (this.getAction().trim().equals("update") && this.getActivId() > 0))) {
			if (this.getName() != null && !this.getName().trim().equals("")) {
				if (this.getDescription() != null && !this.getDescription().trim().equals("")) {
					if (this.getIndicators() != null && !this.getIndicators().trim().equals("")) {
						if (this.getObjId() > 0) {
							
							try {
								Activity activity = null;
								if (this.getAction().trim().equals("insert")) {
									activity = new Activity(this.getName().trim(), 
											this.getDescription().trim(), 
											this.getIndicators().trim());
									
									this.setOptionalFields(activity);
									
									this.activityCon.insert(activity, this.getObjId());
									
								} else if (this.getAction().trim().equals("update")) {
									activity = this.activityCon.findByPK(this.getActivId());
									activity.setName(this.getName().trim());
									activity.setDescription(this.getDescription().trim());
									activity.setIndicators(this.getIndicators().trim());
									
									this.setOptionalFields(activity);
									
									this.activityCon.update(activity);
								}
								this.ajaxResponse.put(SUCCESS, "ok");
								
							} catch (BSCException e) {
								this.ajaxResponse.put(ERROR, e.getMessage());
							}
						} else {
							this.ajaxResponse.put(ERROR, "The id of Objective is required.");
						}
					} else {
						this.ajaxResponse.put(ERROR, "The field indicators is required.");
					}
				} else {
					this.ajaxResponse.put(ERROR, "The field description is required");
				}
			} else {
				this.ajaxResponse.put(ERROR, "The field name is required");
			}
		} else {
			this.ajaxResponse.put(ERROR, "The event action is required. If action is update, send the object ID.");
		}
		
		return SUCCESS;
	}

	private void setOptionalFields(Activity activity) {
		
		if (this.getFrequencyUnit() != null && this.getFrequencyLength() > 0
				&& FrequencyUnit.valueOf(this.getFrequencyUnit().trim()) != null) {
			Frequency frequency = new Frequency(
					FrequencyUnit.valueOf(this.getFrequencyUnit().trim()),
					this.getFrequencyLength());
			activity.setFrequency(frequency);
		}
		
		List<Person> persons = new ArrayList<Person>();
		for (String personName : this.getPersonsNames()) {
			if (personName != null && !personName.trim().equals("")) {
				persons.add(new Person(personName.trim()));
			}
		}
		if (persons.size() > 0) {
			activity.setOwners(persons);
		}
		
		if (this.getTarget() != null && !this.getTarget().trim().equals("")) {
			activity.setTarget(this.getTarget().trim());
		}
		
		if (this.getExpenseType() != null && this.getExpenseValue() != null
				&& this.getExpenseType().length == this.expenseValue.length) {
			
			List<Expense> expenses = new ArrayList<Expense>();
			
			for (int i = 0; i < this.expenseType.length; i++) {
				ExpenseType expType = ExpenseType.valueOf(this.expenseType[i].trim());
				if (expType != null) {
					expenses.add(new Expense(this.expenseValue[i], expType));
				}
			}
			
			if (expenses.size() > 0) {
				activity.setExpenses(expenses);
			}
		}
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getIndicators() {
		return indicators;
	}

	public void setIndicators(String indicators) {
		this.indicators = indicators;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public long getFrequencyLength() {
		return frequencyLength;
	}

	public void setFrequencyLength(long frequencyLength) {
		this.frequencyLength = frequencyLength;
	}

	public String getFrequencyUnit() {
		return frequencyUnit;
	}

	public void setFrequencyUnit(String frequencyUnit) {
		this.frequencyUnit = frequencyUnit;
	}

	public double[] getExpenseValue() {
		return expenseValue;
	}

	public void setExpenseValue(double[] expenseValue) {
		this.expenseValue = expenseValue;
	}

	public String[] getExpenseType() {
		return expenseType;
	}

	public void setExpenseType(String[] expenseType) {
		this.expenseType = expenseType;
	}

	public String[] getPersonsNames() {
		return personsNames;
	}

	public void setPersonsNames(String[] personsNames) {
		this.personsNames = personsNames;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public int getActivId() {
		return activId;
	}

	public void setActivId(int activId) {
		this.activId = activId;
	}

	public Map<String, Object> getAjaxResponse() {
		return ajaxResponse;
	}

	public void setAjaxResponse(Map<String, Object> ajaxResponse) {
		this.ajaxResponse = ajaxResponse;
	}
	
}
